require("../../utils/util.js");

var t = require("../../requests/request.js"), e = require("../../utils/common.js");

getApp();

Page({
    data: {
        winHeight: 0,
        masterlist: {},
        loading: !1,
        initails: [],
        navtype: 0,
        carSerials: [],
        voteHidden: "",
        carSerialHidden: "true",
        addSerailHidden: "",
        serialCount: 0,
        txtCount: 150,
        txtNoteContent: "",
        isShow: !1,
        voteId: "",
        top: 0
    },
    onLoad: function(t) {
        var e = this;
        void 0 != t.navtype && e.setData({
            navtype: t.navtype
        }), wx.getSystemInfo({
            success: function(t) {
                e.setData({
                    winHeight: t.windowHeight
                });
            }
        }), e.loadBrandData();
    },
    loadBrandData: function() {
        var e = this;
        e.setData({
            loading: !0
        }), t.getMasterList(function(t) {
            for (var a = [], i = 0; i < t.length - 1; i++) t[i].CoverPhoto = t[i].CoverPhoto.replace("{0}", "3"), 
            a.contains(t[i].Initial) || a.push(t[i].Initial);
            e.setData({
                initails: a.sort(),
                masterlist: t.sort(function(t, e) {
                    return e.UV - t.UV;
                })
            });
        });
    },
    scalTxtCount: function(t) {
        var e = this, a = t.detail.value.length;
        e.setData({
            txtCount: 150 - a
        }), e.setData({
            txtNoteContent: t.detail.value
        });
    },
    chooseCarSerials: function(t) {
        var e = this, a = e.data.carSerials;
        (!a || a.length <= 0) && (a = []), t.pic.length > 0 && (t.pic = t.pic.replace("{0}", "1"));
        var i = a.filter(function(e) {
            return e.id == t.id;
        });
        !i || i.length <= 0 ? (a.push(t), e.hideCarSerialDialog(), e.showVoteDialog(), e.setData({
            carSerials: a
        }), e.setData({
            serialCount: a.length
        }), e.hideOrShowAddVoteBtn()) : wx.showToast({
            title: "已添加过此车型"
        });
    },
    removeSerial: function(t) {
        var e = this, a = e.data.carSerials;
        if (a && !(a.length <= 0)) {
            var i = a.filter(function(e) {
                return e.id == t.target.dataset.id;
            });
            if (i && i.length > 0) {
                var o = a.indexOf(i[0]);
                o > -1 && a.splice(o, 1), e.setData({
                    carSerials: a
                });
            }
            e.setData({
                serialCount: a.length
            }), e.hideOrShowAddVoteBtn();
        }
    },
    hideOrShowAddVoteBtn: function() {
        var t = this;
        t.data.serialCount >= 6 ? t.setData({
            addSerailHidden: "true"
        }) : t.setData({
            addSerailHidden: ""
        });
    },
    hideVoteDialog: function() {
        wx.setNavigationBarTitle({
            title: "选择品牌"
        }), this.setData({
            voteHidden: "true"
        });
    },
    showVoteDialog: function() {
        wx.setNavigationBarTitle({
            title: "朋友帮选车"
        }), this.setData({
            voteHidden: ""
        });
    },
    hideCarSerialDialog: function() {
        wx.setNavigationBarTitle({
            title: "朋友帮选车"
        }), this.setData({
            carSerialHidden: "true"
        });
    },
    showCarSerialDialog: function() {
        wx.setNavigationBarTitle({
            title: "选择品牌"
        }), this.setData({
            carSerialHidden: ""
        }), this.hideVoteDialog();
    },
    onReady: function() {
        this.brand = this.selectComponent("#brand");
    },
    scrollNum: function(t) {
        var e = t.currentTarget.dataset.num;
        this.setData({
            toNum: e
        });
    },
    errImg: function(t) {
        var a = this;
        e.errImgFun(t, a, "http://res.app.yiche.com/wechatapp/async100x100.png");
    },
    showBrand: function(t) {
        var e = t.currentTarget.dataset.id, a = t.currentTarget.dataset.name;
        this.brand.showDialog(e, a, this.data.navtype);
    },
    hideModel: function() {
        this.brand.hideDialog();
    },
    onMyEvent: function(t) {
        this.chooseCarSerials(t.detail), this.hideModel();
    },
    hide: function() {
        this.setData({
            isShow: !1,
            top: "0"
        }), wx.navigateTo({
            url: "../voteList/voteList"
        });
    },
    addVote: function(e) {
        var a = this, i = {
            serialids: "",
            uid: "",
            note: "",
            username: "",
            cares: "",
            token: "",
            votetype: 0
        }, o = wx.getStorageSync("UserToken");
        if (i.token = o, !a.data.carSerials || a.data.carSerials.length < 2) wx.showToast({
            title: "至少要选择两款车型！"
        }); else {
            if (!i.token || i.token.trim().length <= 0) return wx.showToast({
                title: "完成登录后，发起投票",
                icon: "none",
                duration: 1500
            }), void setTimeout(function() {
                wx.switchTab({
                    url: "../my/my"
                });
            }, 1500);
            var n = "";
            a.data.carSerials.forEach(function(t, e) {
                n += t.id + ",";
            }), n.length > 1 && (n = n.substring(0, n.length - 1)), i.token = o, i.note = a.data.txtNoteContent && a.data.txtNoteContent.length > 0 ? encodeURI(a.data.txtNoteContent) : encodeURI("朋友，是我，我要买车，听听你的意见，我该选哪款?"), 
            i.uid = wx.getStorageSync("opid"), i.votetype = 0, i.serialids = n, t.addVote(i, function(t) {
                a.setData({
                    isShow: !0,
                    voteId: t.Data.VoteID,
                    top: "-1000px"
                });
            }, function(t) {
                console.log(t), wx.showToast({
                    title: "发表投票失败！"
                });
            }, function(t) {
                console.log(t);
            });
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function(t) {
        "button" === t.from ? console.log("来自页面的按钮") : console.log("来自右上角转发菜单");
        var e = "/pages/helpChose/helpChose?voteid=" + t.target.dataset.voteid;
        return console.log(e), {
            title: "朋友帮选车",
            path: e,
            imageUrl: "http://res.app.yiche.com/wechatapp/share_img.png",
            success: function(t) {
                console.log("转发成功", t), wx.navigateTo({
                    url: "../voteList/voteList"
                });
            },
            fail: function(t) {
                console.log("转发失败", t);
            }
        };
    }
});